package ruifeng.zcj.dig.base;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.os.Build;
import android.view.View;
import android.widget.LinearLayout;

import com.jakewharton.rxbinding.view.RxView;

import butterknife.Bind;
import ruifeng.zcj.dig.R;
import ruifeng.zcj.mybase.base.BaseActivity;
import ruifeng.zcj.mybase.base.utils2.TDevice;

/**
 * 是否显示正在加载，加载失败，加载的数据为空
 * Created by Administrator on 2016/9/21 0021.
 */
public abstract class BaseShowHidActivity extends BaseActivity {
    @Bind(R.id.layoutLoading)
    LinearLayout layoutLoading;


    @Bind(R.id.layoutContent)
    LinearLayout mLayoutContent;

    @Bind(R.id.layoutLoadFailed)
    LinearLayout layoutLoadFailed;
    @Bind(R.id.layoutEmpty)
    LinearLayout layoutEmpty;



    public void layoutLoadingVisibility(boolean isVisible) {
        if (layoutLoading != null) {
            RxView.visibility(layoutLoading, View.GONE).call(isVisible);
        }
    }

    public void layoutContentVisibility(boolean isVisible) {
        if (mLayoutContent != null) {
            RxView.visibility(mLayoutContent, View.GONE).call(isVisible);
        }
    }

    public void layoutLoadFailedVisibility(boolean isVisible) {
        if (layoutLoadFailed != null) {
            RxView.visibility(layoutLoadFailed, View.GONE).call(isVisible);
        }
    }

    public void layoutEmptyVisibility(boolean isVisible) {
        if (layoutEmpty != null) {
            RxView.visibility(layoutEmpty, View.GONE).call(isVisible);
        }
    }

    /**
     * Shows the progress UI and hides the login form.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    public void showProgress(final boolean show) {
        // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
        // for very easy animations. If available, use these APIs to fade-in
        // the progress spinner.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);

            mLayoutContent.setVisibility(show ? View.GONE : View.VISIBLE);
            mLayoutContent.animate().setDuration(shortAnimTime).alpha(
                    show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    mLayoutContent.setVisibility(show ? View.GONE : View.VISIBLE);
                }
            });

            layoutLoading.setVisibility(show ? View.VISIBLE : View.GONE);
            layoutLoading.animate().setDuration(shortAnimTime).alpha(
                    show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    layoutLoading.setVisibility(show ? View.VISIBLE : View.GONE);
                }
            });
        } else {
            // The ViewPropertyAnimator APIs are not available, so simply show
            // and hide the relevant UI components.
            layoutLoading.setVisibility(show ? View.VISIBLE : View.GONE);// 显示加载的
            mLayoutContent.setVisibility(show ? View.GONE : View.VISIBLE);// 隐藏内容
        }
    }

    /****
     * 返回true是有网络
     * **/
    public boolean isHasInternet(){
        if (!TDevice.hasInternet()) {
            return false;
        }
        return true;
    }


}
